#include <iostream>
#include <unistd.h>
#include <time.h>
#include <pthread.h>

#include "BlockingQueue.hpp"

using namespace std;

void* Producer(void *args)
{
    Tom::BlockQueue<int>* bq = static_cast<Tom::BlockQueue<int>*>(args);

    while(true)
    {
        sleep(1);
        int num = rand() % 10;

        bq->Push(num);

        cout << "Producer 生产了一个数据: " << num << endl;
        cout << "-------------------------" << endl;
    }
    pthread_exit((void*)0);
}

void* Consumer(void* args)
{
    Tom::BlockQueue<int>* bq = static_cast<Tom::BlockQueue<int>*>(args);

    while(true)
    {
        //sleep(1);
        int num;
        bq->Pop(&num);

        cout << "Consumer 消费了一个数据: " << num << endl;
        cout << "-------------------------" << endl;
    }

    pthread_exit((void*)0);
}

int main()
{
    srand((size_t)time(nullptr));
    Tom::BlockQueue<int>* bq = new Tom::BlockQueue<int>;

    pthread_t pro[2], con[3];
    for(int i = 0; i < 2; i++)
    pthread_create(pro+i, nullptr, Producer, bq);
    
    for(int i = 0; i < 3; i++)
    pthread_create(con+i, nullptr, Consumer, bq);

    for(int i = 0; i < 2; i++)
    pthread_join(pro[i], nullptr);
    
    for(int i = 0; i < 3; i++)
    pthread_join(con[i], nullptr);

    delete bq;
    return 0;
}
